#ifndef HELPER_FUNC
#define HELPER_FUNC

#define PI 3.1415926

float _lookup_offset;

float3 floor_accuracy(const float3 value, const float accuracy)
{
    return floor(value / accuracy) * accuracy;
}

float ray_prepare(float3 ray, inout float3 ray_dir)
{
    // 三个降采样Pass里用到的 处理ray数据
    ray_dir = normalize(ray.xyz); //射线发射方向
    return length(ray.xyz); //模长的倒数
}

float debug_remap(const int count)
{
    const float result = count / 4;
    return result / 255;
}

float4 offset_lookup(sampler2D shadowmap, float2 loc, const float2 offset)
{
    return tex2D(shadowmap, loc.xy + offset / _lookup_offset);
}

// Henyey-Greenstein
// g为光方向的偏心率， a为视线和光线的夹角。
float hg(float a, float g)
{
    float g2 = g * g;
    return (1 - g2) / (4 * 3.1415 * pow(1 + g2 - 2 * g * a, 1.5));
}

float phase(float a, float4 phase_params)
{
    float blend = .5;
    float hgBlend = hg(a, phase_params.x) * (1 - blend) + hg(a, - phase_params.y) * blend;
    return phase_params.z + hgBlend * phase_params.w;
}

float beer_sugar(float d)
{
    return exp(-d);
    return exp(-d) * 0.8 + 0.2;
    // return (1 - exp(-d * 2));
    // return 2 * exp(-d) * (1 - exp(-d * 2));
}

float remap(float v, float minOld, float maxOld, float minNew, float maxNew) {
    return minNew + (v-minOld) * (maxNew - minNew) / (maxOld-minOld);
}

#endif
